#include <stdlib.h>
#include <assert.h>

#include "plist.h"
#include "Monster.h"

MonsterTemplateMgr::PTR gMonsterTemplateMgr = nullptr;

MonsterTemplate::MonsterTemplate()
{
    mBlood = 0;
    mMoney = 0;
    mRes = 0;
    mPoint = 0;
}

int MonsterTemplate::getMoney() const
{
    return mMoney;
}

int MonsterTemplate::getRes() const
{
    return mRes;
}

int MonsterTemplate::getPoint() const
{
    return mPoint;
}

int MonsterTemplate::getBlood() const
{
    return mBlood;
}

void MonsterTemplateMgr::load()
{
    try
    {
        AnyValue* config = parsePlistByFileName("config_role.plist");
        auto& allMonster = config->asDict()->getValue();
        for (auto& monster : allMonster)
        {
            MonsterTemplate::PTR mt = std::make_shared<MonsterTemplate>();

            auto typeID = monster.first;
            std::string idStr = typeID.substr(typeID.find("type")+strlen("type"));
            if (monster.second->asDict()->findValue("score") != nullptr)
            {
                auto score = monster.second->asDict()->findValue("score")->asInt()->getValue();
                auto monsterDrops = monster.second->asDict()->arrayValue("drops");
                if (monsterDrops != nullptr)
                {
                    for (auto& oneDrop : monsterDrops->getValue())
                    {
                        auto dropDict = oneDrop->asDict();
                        auto dropType = dropDict->intValue("itemType");
                        auto value = dropDict->intValue("value");
                        if (dropType == 3)
                        {
                            mt->mMoney = value;
                        }
                        else if (dropType == 4)
                        {
                            mt->mRes = value;
                        }
                        else
                        {
                            //assert(false);
                        }
                    }
                }

                int blood = monster.second->asDict()->intValue("blood", 1);
                mt->mBlood = blood;
                mt->mPoint = score;

                mMonsters[atoi(idStr.c_str())] = mt;
            }
        }

    }
    catch (...)
    {

    }
}

void MonsterTemplateMgr::loadBoss()
{
    try
    {
        AnyValue* config = parsePlistByFileName("config_boss.plist");
        auto& allMonster = config->asDict()->getValue();
        for (auto& monster : allMonster)
        {
            MonsterTemplate::PTR mt = std::make_shared<MonsterTemplate>();

            auto typeID = monster.first;
            std::string idStr = typeID.substr(typeID.find("type") + strlen("type"));
            int blood = monster.second->asDict()->intValue("blood", 1);
            mt->mBlood = blood;

            mMonsters[atoi(idStr.c_str())] = mt;
        }

    }
    catch (...)
    {

    }
}

MonsterTemplate::PTR MonsterTemplateMgr::findMonsterTemplateById(int16_t id)
{
    auto& it = mMonsters.find(id);
    if (it != mMonsters.end())
    {
        return it->second;
    }
    else
    {
        return nullptr;
    }
}

MonsterTemplateMgr::MONSTER_CONTAINER& MonsterTemplateMgr::getMonsterTemplates()
{
    return mMonsters;
}